<?php
namespace Admin\Controller;
// use Think\Controller;
header("Content-type: text/html; charset=utf-8");
use Admin\Common\CommonController;
class AdminController extends CommonController{
	//列出权限角色
    public function roleListAction(){
    	$sql="select t1.id as uid,t1.username,t3.id as ag_id,t3.title,t3.rules from user t1 left join auth_group_access t2 on t1.id=t2.uid left join auth_group t3 on t2.group_id=t3.id";
    	$data_m=M();
    	$rule_group_res=$data_m->query($sql);
    	// var_dump($rule_group_res);
    	// exit();
    	$this->assign('rule_group_res',$rule_group_res);
    	$this->display();
    }

    //添加管理员角色
    public function roleAddAction(){
    	if(IS_POST){
    		// var_dump(I('post.'));
    		$usernameid=I('post.usernameid');//传入用户ID
    		$rolename=I('post.rolename');//传入管理角色名称
    		$role_list_arr=I('post.role_list');
    		$role_list_string=implode(',',$role_list_arr);//将数组转换成字符串
    		$descript=I('post.descript');//描述权限
    		$status = 1;
    		$data['title']=$rolename;
    		$data['status']=$status;
    		$data['rules']=$role_list_string;
    		$data['comment']=$descript;
    		$auth_group=M('auth_group');
    		$auth_group_res=$auth_group->add($data);
    		// var_dump($auth_group_res);
    		// exit();
    		if($auth_group_res){
    			$sql="insert into auth_group_access(uid,group_id) values('$usernameid','$auth_group_res')";
    			$data_m=M();
    			$res=$data_m->execute($sql);
    			if($res){
    				$this->success("添加权限成功",U('Admin/Category/userSuccess'),2);
    			}else{
    				$this->error("添加权限失败",U('Admin/Category/themeList'),2);
    			}
    		}
    	}else{
    		$sql="select title,id from auth_rule";
	    	$data_m=M();
	    	$rule_res=$data_m->query($sql);
	    	$user_sql="select * from user";
	    	$user_res=$data_m->query($user_sql);
	    	// var_dump($user_res);
	    	// exit();
	    	$this->assign('user_res',$user_res);
	    	$this->assign('rule_res',$rule_res);
	    	$this->display();
    	}   	
    }

    //编辑管理员角色
    public function roleEditAction(){
    	if(IS_POST){
    		// var_dump(I('post.'));
    		// exit();
    		$roleid=I('post.roleid');
    		$rolename=I('post.rolename');//传入管理角色名称
    		$role_list_arr=I('post.role_list');
    		$role_list_string=implode(',',$role_list_arr);//将数组转换成字符串
    		$descript=I('post.descript');//描述权限
    		$status = 1;
    		$sql="update auth_group set title='$rolename',status='$status',rules='$role_list_string',comment='$descript' where id=$roleid";
    		$data_m=M();
    		$res=$data_m->execute($sql);
			if($res){
				$this->success("修改权限成功",U('Admin/Category/userSuccess'),2);
			}else{
				$this->error("添加权限失败",U('Admin/Category/themeList'),2);
			}
    	}else{
    		$sql="select title,id from auth_rule";
	    	$data_m=M();
	    	$rule_res=$data_m->query($sql);
	    	$user_sql="select * from user";
	    	$user_res=$data_m->query($user_sql);
	    	//获取需要修改的用户角色数据
	    	$roleid=I('get.id');
	    	$sql="select t1.id,t1.title,t1.rules,t1.comment,t3.username from auth_group t1 left join auth_group_access t2 on t1.id=t2.group_id left join user t3 on t2.uid=t3.id where t1.id='$roleid'";
	    	$data_m=M();
	    	$res=$data_m->query($sql);
	    	$res=current($res);
	    	//分配到模板中
	    	$this->assign('res',$res);
	    	$this->assign('user_res',$user_res);
	    	$this->assign('rule_res',$rule_res);
	    	$this->display();
    	}
    }

    //删除管理员角色
    public function roleDelAction(){
    	$roleuserid=I('post.uid');
    	$sql="delete from auth_group_access where uid='$roleuserid'";//一个用户只能对应一种角色，所以只需要找到角色对应的用户ID删除即可
    	
    	// var_dump($sql);
    	// exit();
    	$data_m=M();
    	$res=$data_m->execute($sql);
    	$arr = array('success' =>$res);
    	echo json_encode($arr);
    	
    }


    //权限管理列表和添加列表
    public function adminPemissionAction(){
        if(IS_POST){
            $PemissionName=I('post.PemissionName');
            $ziduanName=I('post.ziduanName');
            $type=1;
            $status=1;
            $data_m=M();
            //字段名不可以重复
            $ziduan="select name from auth_rule where name='$ziduanName'";
            $ziduan_res= $data_m->query($ziduan);
            // var_dump($ziduan_res);
            // exit();
            //不为空说明数据库中有相同的字段名
            if(!empty($ziduan_res)){
                 $arr = array('error' =>"字段名不可以重复");   
                 echo json_encode($arr); 
            }else{
                $sql="insert into auth_rule(name,title,type,status) values('$ziduanName','$PemissionName','$type','$status')";
                $res=$data_m->execute($sql);
                $lastId=$data_m->getLastInsID();
                // if($res){
                //     $arr = array('lastId'=>$lastId,'PemissionName' => $PemissionName,'ziduanName'=>$ziduanName,'type'=>$type,'status'=>$status,'success'=>$res);
                // }
                 $arr = array('success'=>$res);
                echo json_encode($arr);
            }
            
        }else{
            $sql="select * from auth_rule";
            $data_m=M();
            $res=$data_m->query($sql);
            // var_dump($res);
            // exit();
            $this->assign('res',$res);
            $this->display();
        }
        
    }

    //权限管理删除
    public function PemissionDelAction(){
        $permissionid=I('POST.permissionid');
        $sql="delete from auth_rule where id='$permissionid'";
        $data_m=M();
        $res=$data_m->execute($sql);
        $arr = array('success' =>$res);
        echo json_encode($arr);

    }




}
?>